﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using XbsWebAPI.Application.QueryModel;
using XbsWebAPI.Core.Repositories;
using XbsWebAPI.EntityFrameworkCore.Entities;

namespace XbsWebAPI.Application
{
    public class CampAppService : ICampAppService
    {
        private readonly ICampRepository _campRepository;
        private readonly IMapper _mapper;

        public CampAppService(ICampRepository campRepository, IMapper mapper)
        {
            _campRepository = campRepository;
            _mapper = mapper;
        }

        public Task<ResultDto<string>> CreateCampAsync(CreateCampDto model)
        {
            var result = new ResultDto<string>();

            try
            {
                var entity = _mapper.Map<Camp>(model);
                _campRepository.CreateAsync(entity);

                result.Code = 0;
                result.Message = "";
                result.Result = "";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public Task<ResultDto<string>> EditCampAsync(EditCampDto model)
        {
            var result = new ResultDto<string>();
            try
            {
                var entity = _mapper.Map<Camp>(model);
                _campRepository.UpdateAsync(entity);
                result.Code = 0;
                result.Message = "";
                result.Result = "";
            }
            catch (Exception e)
            {
                result.Code= -1;
                result.Message= e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }
            return Task.FromResult(result);
        }

        public ResultDto<GetCampDto> GetCampById(Guid id)
        {
            var result = new ResultDto<GetCampDto>();
            try
            {
                var model = _campRepository
            .Query(a => a.Id == id)
            .Include(a => a.CampImages).First();
                var entity = _mapper.Map<GetCampDto>(model);

                result.Code = 0;
                result.Message = "";
                result.Result = entity;
            }
            catch (Exception e)
            {
                result.Code = -1;
                 result.Message= e.Message;
                result.Result = null;
                return result;
            }

            return result;
        }

        public ResultDto<PageListDto<ListCampDto>> GetPagedList(QueryCamp query)
        {
            var result = new ResultDto<PageListDto<ListCampDto>>();

            try
            {
                var camps = _campRepository.AsQueryable();

                if (!string.IsNullOrEmpty(query.Name))
                {
                    camps = camps.Where(a => a.Name.Contains(query.Name));
                }

                camps = camps.OrderByDescending(a => a.CreationDate);
                var pagedList = camps.ToPagedList(query.PageNumber ?? 1, query.PageSize ?? 10);
                PageListDto<ListCampDto> pagedDto = new PageListDto<ListCampDto>()
                {
                    PageNumber = pagedList.PageNumber,
                    PageSize = pagedList.PageSize,
                    HasPreviousPage = pagedList.HasPreviousPage,
                    HasNextPage = pagedList.HasNextPage,
                    TotalCount = pagedList.TotalItemCount
                };
                var dtos = pagedList.Select(e => _mapper.Map<ListCampDto>(e)).ToList();
                pagedDto.Dtos = dtos;

                result.Code = 0;
                result.Message = "操作成功";
                result.Result = pagedDto;
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = null;
                return result;
            }

            return result;
        }

        public ResultDto<List<SelectCampDto>> GetSelectCamps()
        {
            var result = new ResultDto<List<SelectCampDto>>();

            try
            {
                var camps = _campRepository.AsQueryable().ToList();
                var campsDtos = camps.Select(e => _mapper.Map<SelectCampDto>(e)).ToList();

                result.Code = 0;
                result.Message = "操作成功";
                result.Result = campsDtos;
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = null;
                return result;
            }


            return result;
        }

        public Task<ResultDto<string>> RemoveCampAsync(Guid id)
        {
            var result = new ResultDto<string>();

            try
            {
                _campRepository.DeleteAsync(id);
                result.Code = 0;
                result.Message = "操作成功";
                result.Result = "";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                result.Result = "操作失败";
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }
    }
}
